// 三、完成后续四个练习
const { Maybe, Container } = require('./support');
const fp = require('lodash/fp');

// 练习1
const maybe = Maybe.of([5, 6, 1]);
const ex1 = () => {
  return maybe.map(value => {
    return fp.reduce(fp.add, 0, value);
  })
};
console.log(ex1()._value);

// 练习2
const xs = Container.of(['do', 'ray', 'me', 'fa', 'so', 'la', 'ti', 'do'])
const ex2 = () => {
  return xs.map(value => {
    return fp.first(value);
  });
};
console.log(ex2()._value);

// 练习3
const safeProp = fp.curry(function(x, o) {
  return Maybe.of(o[x]);
});
const user = { id: 2, name: 'Albert' };
const ex3 = () => {
  return safeProp('name')(user).map(value => {
    return fp.first(value);
  });
};
console.log(ex3()._value);

// 练习4
const ex4 = function(n) {
  if (n) {
    return parseInt(n);
  }
};

const newEx4 = function(n) {
  return Maybe.of(n).map(parseInt);
};
console.log(newEx4(null)._value);
console.log(newEx4(1.22)._value);
console.log(newEx4(0.1)._value);
